package com.elh.aop.interceptor;


import com.elh.aop.aspect.Aspect;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * @author liulei
 **/
public class JdkInterceptor implements InvocationHandler {

    private Object target;

    private Aspect aspect;

    public JdkInterceptor(Object target, Aspect aspect) {
        this.target = target;
        this.aspect = aspect;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args)  throws Throwable{
        Object result = null;
        if(aspect.before(method,proxy,args)){
            try {
                method.setAccessible(true);
                result = method.invoke(proxy,args);
            } catch (Throwable e) {
                aspect.throwException(method,proxy,args);
                throw e;
            }
        }
        if(aspect.after(method,proxy,args)){
            return result;
        }
        return result;
    }



}
